<?php
/**
 * 公司控制器
 * User: guojing
 * Date: 2020/4/7
 * Time: 11:18
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\api\model\Company as CompanyModel;
use think\Cookie;
use think\Db;
use app\api\model\User as UserModel;
use think\Log;

class Company extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function index()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        if ($userId <= 0) {
            $this->error('请重新登录', [],401);
        }
        //获取用户的企业信息
        $company = CompanyModel::findInfoByCondition(['user_id' => $userId]);
        $res = [
            'is_first' => $company ? 0 : 1,
            'company_card' => $company['company_card'],
            'company_name' => $company['company_name'],
            'company_social_credit_code' => $company['company_social_credit_code'],
            'company_deputy' => $company['company_deputy'],
            'company_lianxiren' => $company['company_lianxiren'],
            'company_position' => $company['company_position']
        ];
        $this->success('success', $res);
    }

    /**
     * 修改企业信息
     */
    public function modifyCompany()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        if ($userId <= 0) {
            $this->error('请重新登录', [],401);
        }

        $companyCard = $this->request->post('company_card', '');
        $companyName = $this->request->post('company_name', '');
        $companySocialCreditCode = $this->request->post('company_social_credit_code', '');
        $companyDeputy = $this->request->post('company_deputy', '');
        $companyContact = $this->request->post('company_lianxiren', '');
        $companyPosition = $this->request->post('company_position', '');
        if (!$companyCard) {
            $this->error(__('上传营业执照为必要信息，不能留空！'));
        }
        if (!$companyName) {
            $this->error(__('公司名称为必要信息，不能留空！'));
        }
        if (!$companySocialCreditCode) {
            $this->error(__('统一社会信用代码为必要信息，不能留空！'));
        }
        if (!$companyDeputy) {
            $this->error(__('法人代表为必要信息，不能留空！'));
        }

        //用户类型
        $uType = $this->request->post('u_type', 0, 'intval');

        $data = [
            'company_card' => $companyCard,
            'company_name' => $companyName,
            'company_social_credit_code' => $companySocialCreditCode,
            'company_deputy' => $companyDeputy,
            'company_lianxiren' => $companyContact,
            'company_position' => $companyPosition,
            'requesting' => 1
        ];
        if ($uType == 2) {  // 用户类型为企业,默认审核通过
            $data['requesting'] = 2;
        }

        //获取用户的企业信息
        $company = CompanyModel::findInfoByCondition(['user_id' => $userId]);

        if ($company) {
            //更新
            CompanyModel::saveData($company['id'], $data);
        } else {    //添加
            $data['user_id'] = $userId;
            $company = CompanyModel::addRecord($data);
            if (!$company) {
                $this->error(__('提交失败'));
            }
        }

        if ($uType == 2) {  // 用户类型为企业
            //企业注册完成
            UserModel::saveData($userId, ['is_company_reg' => 1]);
        }

        $this->success('success', []);
    }

}